package org.dromara.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.json.JSONUtil;
import io.rong.RongCloud;
import io.rong.models.message.PrivateMessage;
import io.rong.models.response.MessageResult;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.AppWithdrawRecordVo;
import org.dromara.system.domain.bo.AppWithdrawRecordBo;
import org.dromara.system.service.IAppWithdrawRecordService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 用户提现记录
 *
 * @author Lion Li
 * @date 2025-09-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/withdrawRecord")
public class AppWithdrawRecordController extends BaseController {

    private final IAppWithdrawRecordService appWithdrawRecordService;

    @Autowired
    private RongCloud rongCloud;

    @GetMapping("/sendMessage")
    public String sendMessage() throws Exception {
        CourseMessage courseMsg = new CourseMessage(
            "感恩遇见！您好，我是" + "data1.getTeacherName()" + "老师。<br>" +
                "预约时间：<span style='color: green;'>" + "data1.getSpecificTime()" + "</span><br>" +
                "<span style='color: green;'>" + "data1.getServiceDate()" + "</span><br>" +
                "请确认预约的时间是否在有线且网络畅通呢？",
            "李华",
            "123",
            "11:30-12:00",
            "11-30"
        );
        HashMap<String,String> map =new HashMap<>();
        map.put("teacher", "李华");
        map.put("specificTime", "11:30-12:00");
        map.put("serviceDate", "2025-11-30");
        String jsonStr = JSONUtil.toJsonStr(map);
        PrivateMessage privateMessage = new PrivateMessage()
            .setSenderId("212121212121")
            .setTargetId(new String[]{"1861665398507266050"})
            // ✅ 这里定义自定义消息类型标识符
            .setObjectName("RC:CourseMsg")
            .setContent(courseMsg)
            .setPushContent(jsonStr)
            .setPushData("{\"pushData\":\"course\"}")
            .setIsPersisted(1)
            .setIsCounted(1)
            .setNeedReadReceipt(1)
            .setExpansion(true)
            .setExtraContent(map)
            .setIsIncludeSender(0);
        // Method to send a one-to-one chat message
        MessageResult publishPrivateResult = rongCloud.message.msgPrivate.send(privateMessage);
        System.out.println("sendPrivate:  " + publishPrivateResult.toString());
        return publishPrivateResult.toString();
    }

    /**
     * 查询用户提现记录列表
     */
    @SaCheckPermission("system:withdrawRecord:list")
    @GetMapping("/list")
    public TableDataInfo<AppWithdrawRecordVo> list(AppWithdrawRecordBo bo, PageQuery pageQuery) {
        return appWithdrawRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户提现记录列表
     */
    @SaCheckPermission("system:withdrawRecord:export")
    @Log(title = "用户提现记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppWithdrawRecordBo bo, HttpServletResponse response) {
        List<AppWithdrawRecordVo> list = appWithdrawRecordService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户提现记录", AppWithdrawRecordVo.class, response);
    }

    /**
     * 获取用户提现记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:withdrawRecord:query")
    @GetMapping("/{id}")
    public R<AppWithdrawRecordVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(appWithdrawRecordService.queryById(id));
    }

    /**
     * 新增用户提现记录
     */
    @SaCheckPermission("system:withdrawRecord:add")
    @Log(title = "用户提现记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppWithdrawRecordBo bo) {
        return toAjax(appWithdrawRecordService.insertByBo(bo));
    }

    /**
     * 修改用户提现记录
     */
    @SaCheckPermission("system:withdrawRecord:edit")
    @Log(title = "用户提现记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppWithdrawRecordBo bo) {
        return toAjax(appWithdrawRecordService.updateByBo(bo));
    }

    /**
     * 删除用户提现记录
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:withdrawRecord:remove")
    @Log(title = "用户提现记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(appWithdrawRecordService.deleteWithValidByIds(List.of(ids), true));
    }
}
